package cn.ybx.seaIslands;

import cn.ybx.seaIslands.exceptions.MapFormatErrorException;
import cn.ybx.seaIslands.models.Point;

import java.io.File;
import java.io.IOException;

public class SeaIslandsApp {
    public static void main(String[] args) {
        if (args.length < 1) {
            System.err.println("[ERROR] should give a map file");
            System.exit(-1);
        }
        //for test
//        File mapFile = new File("src/main/resources/map");
        File mapFile = new File(args[0]);
        if (!mapFile.exists()) {
            System.err.println("[ERROR] no map");
            System.exit(-1);
        }

        MapEngine mapEngine = null;
        try {
            mapEngine = MapEngine.getInstance(mapFile, "rectangle");
        } catch (IOException | MapFormatErrorException e) {
            System.err.println("[ERROR] " + e.getMessage());
            System.exit(-1);
        }

        //print the map
        System.out.println("# print the map");
        mapEngine.printGridMap();

        System.out.println("# test results");
        //get island size
        int res1 = mapEngine.getIslandSizeByCoordinates(new Point(0, 3));
        System.out.println("#1 size of (0,3):" + (res1 == -1 ? "NO ISLAND" : res1));
        int res2 = mapEngine.getIslandSizeByCoordinates(new Point(4, 5));
        System.out.println("#2 size of (4,5):" + (res2 == -1 ? "NO ISLAND" : res2));
        int res3 = mapEngine.getIslandSizeByCoordinates(new Point(7, 2));
        System.out.println("#3 size of (7,2):" + (res3 == -1 ? "NO ISLAND" : res3));

        //tell is in the same island
        int res4 = mapEngine.isInTheSameIsland(new Point(5, 5), new Point(3, 6));
        System.out.println("#4 (5,5) and (3,6):" + (res4 == -1 ? "NO ISLAND" : (res4 == 0 ? "DIFFERENT" : "SAME")));
        int res5 = mapEngine.isInTheSameIsland(new Point(2, 8), new Point(7, 8));
        System.out.println("#5 (2,8) and (7,8):" + (res5 == -1 ? "NO ISLAND" : (res5 == 0 ? "DIFFERENT" : "SAME")));
        int res6 = mapEngine.isInTheSameIsland(new Point(9, 2), new Point(4, 0));
        System.out.println("#6 (9,2) and (4,0):" + (res6 == -1 ? "NO ISLAND" : (res6 == 0 ? "DIFFERENT" : "SAME")));

        //num of islands
        System.out.println("#7 num of islands:" + mapEngine.getSeaIslandsMap().getIslands().size());

        //max size
        System.out.println("#8 max size island:" + mapEngine.getTheMaxSizeIsland().getLandCoordinates().size());

    }
}
